# coding: utf-8

from time import time, localtime
from calendar import monthcalendar

def application(environ, start_response):
    start_response('200 OK', [('content-type', 'text/html; charset=utf-8')])
    ano, mes, hoje = localtime(time())[:3]

    yield '<HTML><TITLE>Calendário Dinâmico</TITLE>'
    yield '<BODY>'
    yield '<CENTER>'
    yield '<H1>Calendário de %02d/%04d</H1>' % (mes, ano)
    yield '<TABLE>'
    yield '<TR>'
    for dia_sem in ['seg','ter','qua','qui','sex','sab','dom']:
        if dia_sem in ['sab','dom']: 
            bgcolor = 'green'
        else: 
            bgcolor = 'blue'
        yield '<TH WIDTH="45" BGCOLOR="%s">' % bgcolor
        yield '<H3>%s</H3></TH>' % dia_sem
    yield '</TR>'
    for semana in monthcalendar(ano, mes):
        yield '<TR>'
        num_dia_sem = 0
        for dia in semana:
            if dia == hoje:
                bgcolor = 'pink'
            elif num_dia_sem >= 5:
                bgcolor = 'lightgreen'
            else:
                bgcolor = 'lightblue'
            yield '<TD ALIGN="RIGHT" BGCOLOR="%s">' % bgcolor
            if dia != 0:
                yield '<H2>%2d</H2>' % dia
            yield '</TD>'
            num_dia_sem = num_dia_sem + 1
        yield '</TR>'
    yield '</TABLE></CENTER>'
    yield '</BODY></HTML>'



if __name__ == '__main__':
    from paste import httpserver
    httpserver.serve(application, host='127.0.0.1', port='8080')

